<?php
namespace App\Service\Admin;

class TongjiService1003 extends BaseService
{
    //统计

    //个人
    public static function person($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWhItem.' as a')
            ->select('a.*', 'b.item_id', 'b.in_day')
            ->leftJoin(self::$dbYwWh.' as b', 'a.wh_id', '=', 'b.id')
            ->where('a.del', 0)
            ->where('b.del', 0);
        // $db = $db->where('a.delay', '>', 0);
        $db = $db->whereIn('b.status', [5,6,7]);
        if ($pas['depart_id']) {
            $db = $db->where('a.depart_id', $pas['depart_id']);
        }
        if ($pas['staff_id']) {
            $db = $db->where('a.uid', $pas['staff_id']);
        }
        if ($pas['month']) {
            $month = strlen($pas['month'])<2 ? '0'.$pas['month'] : $pas['month'];
            $starttime = $pas['year'].'-'.$month.'-01 00:00:01';
            $endtime = $pas['year'].'-'.$month.'-31 23:59:59';
            // var_dump($starttime, $endtime);
            $db = $db->where('a.uptime', '>=', $starttime);
            $db = $db->where('a.uptime', '<=', $endtime);
        }
        $db = $db2 = $db->groupBy('a.config_whid');
        // ddd($db->get(), $db->count('a.id'));
        // $total = $db->count();
        $total = count($db2->get());
        $res = $db->orderBy('a.depart_id', 'asc')
            ->orderBy('a.uid', 'asc')
            ->orderBy('a.config_whid', 'asc')
            ->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '' : $departOne['title'];
            $staffOne = (array) self::dbConn(self::$dbStaff)
                ->where('id', $v->uid)
                ->first();
            $staff_name = empty($staffOne) ? '' : $staffOne['title'];
            // $whOne = (array) self::dbConn(self::$dbYwWh)
            //     ->where('id', $v->wh_id)
            //     ->first();
            // $item_id = empty($whOne) ? '' : $whOne['item_id'];
            $itemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($itemOne) ? '' : $itemOne['title'];
            $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                ->where('id', $v->config_whid)
                ->first();
            $wh_name = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
            $delay = self::dbConn(self::$dbYwWhItem.' as a')
                ->select('a.*')
                ->leftJoin(self::$dbYwWh.' as b', 'a.wh_id', '=', 'b.id')
                ->where('b.item_id', $v->item_id)
                ->where('a.del', 0)
                ->where('a.depart_id', $v->depart_id)
                ->where('a.uid', $v->uid)
                // ->where('a.wh_id', $v->wh_id)
                ->where('a.config_whid', $v->config_whid)
                // ->where('a.uptime', '>=', date('Y-m-d', strtotime($v->uptime)).' 00:00:01')
                // ->where('a.uptime', '<=', date('Y-m-d', strtotime($v->uptime)).' 23:59:59')
                ->where('b.in_day', $v->in_day)
                ->sum('a.delay');
                // ->get();
            // print_r($delay);
            $shi = floor($delay / 60);
            $fen = $delay - $shi * 60;
            $delay_str = $shi . '小时' . $fen . '分钟';
            $arys[] = [
                'id' => $v->id, 'depart_name' => $depart_name,
                'uname' => $staff_name, 'item_name' => $item_name,
                'wh_name' => $wh_name, 'delay' => $delay_str,
                'in_day' => explode(' ', $v->uptime)[0],
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //项目
    public static function item($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWhItem.' as a')
            ->select('a.*', 'b.item_id', 'b.in_day')
            ->leftJoin(self::$dbYwWh.' as b', 'a.wh_id', '=', 'b.id')
            ->where('a.del', 0)
            // ->where('a.delay', '>', 0)
            ->where('b.del', 0);
        $db = $db->whereIn('b.status', [5,6,7]);
        if ($pas['depart_id']) {
            $db = $db->where('a.depart_id', $pas['depart_id']);
        }
        if ($pas['staff_id']) {
            $db = $db->where('a.uid', $pas['staff_id']);
        }
        $db = $db2 = $db->groupBy('a.config_whid');
        // $total = $db->count();
        $total = count($db2->get());
        $res = $db->orderBy('a.depart_id', 'asc')
            ->orderBy('a.uid', 'asc')
            ->orderBy('a.config_whid', 'asc')
            ->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '' : $departOne['title'];
            $cfgItemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($cfgItemOne) ? '' : $cfgItemOne['title'];
            $itemOnes = self::dbConn(self::$dbYwWhItem.' as a')
                ->select('a.*')
                ->leftJoin(self::$dbYwWh.' as b', 'a.wh_id', '=', 'b.id')
                ->where('b.item_id', $v->item_id)
                ->where('a.del', 0)
                ->where('a.depart_id', $v->depart_id)
                ->where('a.uid', $v->uid)
                ->where('a.config_whid', $v->config_whid)
                ->where('b.in_day', $v->in_day)
                ->skip(0)->take(5)
                ->orderBy('id', 'desc')
                ->get();
            $delay_pid = 0;
            $itemList = [];
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $cfgWhName = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                    $shi = floor($vv->delay / 60);
                    $fen = $vv->delay - ($shi * 60);
                    $delay_str = $shi . '小时' . $fen . '分钟';
                    $delay_pid += $vv->delay;
                    $itemList[] = [
                        'id' => $vv->id, 'wh_name' => $cfgWhName,
                        'delay_str' => $delay_str,
                    ];
                }
            }
            $shi_pid = floor($delay_pid / 60);
            $fen_pid = $delay_pid - ($shi_pid * 60);
            $delay_pid_str = $shi_pid . '小时' . $fen_pid . '分钟';
            $arys[] = [
                'id' => $v->id, 'depart_name' => $depart_name,
                'item_name' => $item_name,
                'whList' => $itemList, 'delay_str' => $delay_pid_str,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    // //项目
    // public static function item($pages, $pas)
    // {
    //     $start = ($pages['start'] - 1) * $pages['limit'];
    //     $db = self::dbConn(self::$dbConfigItem.' as a')
    //         ->select('a.*')
    //         // ->leftJoin(self::$dbYwWhItem.' as b', 'a.id', '=', 'b.config_whid')
    //         // ->leftJoin(self::$dbYwWh.' as c', 'c.item_id', '=', 'a.id')
    //         // ->where('c.del', 0)
    //         // ->where('c.delay', '>', 0)
    //         ->where('a.del', 0);
    //     // if ($pas['depart_id']) {
    //     //     $db = $db->where('a.depart_id', $pas['depart_id']);
    //     // }
    //     $total = $db->count();
    //     $res = $db->skip($start)->take($pages['limit'])
    //         ->orderBy('a.id', 'asc')
    //         ->get();
    //     if (!count($res)) {
    //         return self::toback(-2, '没有数据');
    //     }
    //     $arys = [];
    //     foreach ($res as $v) {
    //         $departOne = (array) self::dbConn(self::$dbStDepart)
    //             ->where('id', $v->depart_id)
    //             ->first();
    //         $depart_name = empty($departOne) ? '' : $departOne['title'];
    //         $itemOnes = self::dbConn(self::$dbYwWhItem)
    //             ->where('del', 0)
    //             ->where('config_whid', $v->id)
    //             ->skip(0)->take(5)
    //             ->orderBy('id', 'desc')
    //             ->get();
    //         $delay_pid = 0;
    //         $itemList = [];
    //         if (count($itemOnes)) {
    //             foreach ($itemOnes as $vv) {
    //                 $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
    //                     ->where('id', $vv->config_whid)
    //                     ->first();
    //                 $cfgWhName = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
    //                 $shi = floor($vv->delay / 60);
    //                 $fen = $vv->delay - ($shi * 60);
    //                 $delay_str = $shi . '小时' . $fen . '分钟';
    //                 $delay_pid += $vv->delay;
    //                 $itemList[] = [
    //                     'id' => $vv->id, 'wh_name' => $cfgWhName,
    //                     'delay_str' => $delay_str,
    //                 ];
    //             }
    //         }
    //         $shi_pid = floor($delay_pid / 60);
    //         $fen_pid = $delay_pid - ($shi_pid * 60);
    //         $delay_pid_str = $shi_pid . '小时' . $fen_pid . '分钟';
    //         $arys[] = [
    //             'id' => $v->id, 'item_name' => $v->title,
    //             'depart_name' => $depart_name,
    //             'delay_str' => $delay_pid_str, 
    //             'whList' => $itemList,
    //         ];
    //     }
    //     return self::toback(0, '', $arys, $total);
    // }

    //审核员
    public static function all($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbConfigItem)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'asc')
            ->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $itemOnes = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('config_whid', $v->id)
                ->skip(0)->take(5)
                ->orderBy('id', 'desc')
                ->get();
            $delay_pid = 0;
            $itemList = [];
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $cfgWhName = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                    $shi = floor($vv->delay / 60);
                    $fen = $vv->delay - ($shi * 60);
                    $delay_str = $shi . '小时' . $fen . '分钟';
                    $delay_pid += $vv->delay;
                    $itemList[] = [
                        'id' => $vv->id, 'wh_name' => $cfgWhName,
                        'delay_str' => $delay_str,
                    ];
                }
            }
            $shi_pid = floor($delay_pid / 60);
            $fen_pid = $delay_pid - ($shi_pid * 60);
            $delay_pid_str = $shi_pid . '小时' . $fen_pid . '分钟';
            $arys[] = [
                'id' => $v->id, 'item_name' => $v->title,
                'delay_str' => $delay_pid_str, 'whList' => $itemList,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //环比
    public static function huan($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbConfigItem)->where('del', 0);
        if ($pas['depart_id']) {
            $db = $db->where('depart_id', $pas['depart_id']);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'asc')
            ->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '' : $departOne['title'];
            $itemOnes = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('config_whid', $v->id)
                ->orderBy('uptime', 'desc')
                ->skip(0)->take(5)
                ->get();
            $delay_pid = 0;
            $itemList = [];
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $cfgWhName = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                    $shi = floor($vv->delay / 60);
                    $fen = $vv->delay - ($shi * 60);
                    $delay_str = $shi . '小时' . $fen . '分钟';
                    $delay_pid += $vv->delay;
                    $itemList[] = [
                        'id' => $vv->id, 'wh_name' => $cfgWhName,
                        'delay_str' => $delay_str,
                    ];
                }
            }
            $shi_pid = floor($delay_pid / 60);
            $fen_pid = $delay_pid - ($shi_pid * 60);
            $delay_pid_str = $shi_pid . '小时' . $fen_pid . '分钟';
            // 同比，上一年
            $start = $end = date('Y-m-d');
            if (count($itemOnes)) {
                $start = $itemOnes[count($itemOnes)-1]->uptime;
                $end = $itemOnes[0]->uptime;
            }
            $start = strtotime($start) - 3600 * 24 * 365;
            $end = strtotime($end) - 3600 * 24 * 365;
            $start = date('Y-m-d', $start) . ' 00:00:01';
            $end = date('Y-m-d', $end) . ' 23:59:59';
            $delay_year = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('config_whid', $v->id)
                ->where('uptime', '>=', $start)
                ->where('uptime', '<=', $end)
                ->orderBy('uptime', 'desc')
                ->skip(0)->take(5)
                ->sum('delay');
            if ($delay_year) {
                $tongbi = number_format($delay_pid / $delay_year, 2);
            } else {
                $tongbi = $delay_pid * 100;
            }
            $tongbi = $tongbi>0 ? '+' . $tongbi : '-' . $tongbi;
            // 环比，上一个月
            $start = $end = date('Y-m-d');
            if (count($itemOnes)) {
                $start = $itemOnes[count($itemOnes)-1]->uptime;
                $end = $itemOnes[0]->uptime;
            }
            $start = strtotime($start) - 3600 * 24 * 30;
            $end = strtotime($end) - 3600 * 24 * 30;
            $start = date('Y-m-d', $start) . ' 00:00:01';
            $end = date('Y-m-d', $end) . ' 23:59:59';
            $delay_month = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('config_whid', $v->id)
                ->where('uptime', '>=', $start)
                ->where('uptime', '<=', $end)
                ->orderBy('uptime', 'desc')
                ->skip(0)->take(5)
                ->sum('delay');
            if ($delay_month) {
                $huanbi = number_format($delay_pid / $delay_month, 2);
            } else {
                $huanbi = $delay_pid * 100;
            }
            $huanbi = $huanbi>0 ? '+' . $huanbi : '-' . $huanbi;
            $arys[] = [
                'id' => $v->id, 'item_name' => $v->title,
                'depart_name' => $depart_name,
                'delay_str' => $delay_pid_str, 
                'whList' => $itemList,
                // 同比上一年，环比上一月
                'delay_pid' => $delay_pid, 'delay_year' => $delay_year,
                'tongbi' => $tongbi, 'huanbi' => $huanbi,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //图
    public static function tu()
    {
        $leadOne = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_lead', 1)
            ->first();
        if (empty($leadOne)) {
            return self::toback(-2, '没有权限');
        }
        $arys = [
            'bing' => [['xxx', 10], ['zzz', 20]], //项目占比
            'zhu' => [ //工时占比
                'title' => ['张三', '李四'],
                'val' => [[10], [50]],
            ],
        ];
//        return self::toback(0, '', $arys);
        $itemOnes = self::dbConn(self::$dbConfigItem)
            ->where('del', 0)
            ->get();
        if (count($itemOnes)) {
            foreach ($itemOnes as $v) {
                $delay = self::dbConn(self::$dbYwWh)
                    ->where('del', 0)
                    ->where('item_id', 0)
                    ->sum('delay');
                $arys['bing'][] = [
                    $v->title, $delay,
                ];
            }
        }
        $staffOnes = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_lead', 0)
            ->get();
        if (count($staffOnes)) {
            foreach ($staffOnes as $v) {
                $delay = self::dbConn(self::$dbYwWhItem)
                    ->where('del', 0)
                    ->where('uid', 0)
                    ->sum('delay');
                $arys['zhu']['title'][] = $v->title;
                $arys['zhu']['val'][] = [$delay];
            }
        }
        return self::toback(0, '', $arys);
    }

    //选项
    public static function getOption()
    {
        $arys = ['staff' => []];
        $arys['staff'] = self::dbConn(self::$dbStaff)
            ->select('id', 'title')
            ->where('is_able', 0)
            ->get();
        $arys['depart'] = self::dbConn(self::$dbStDepart)
            ->select('id', 'title')
            ->where('title', '<>', '')
            ->where('del', 0)
            ->get();
        return self::toback(0, '', $arys);
    }

//    //获取员工月底数据
//    public static function staffMonthList($staff_id,  $year, $month)
//    {
//        $staffOne = (array) self::dbConn(self::$dbStaff)
//            ->where('is_able', 0)
//            ->where('id', $staff_id)
//            ->first();
//        if (empty($staffOne)) {
//            return self::toback(-2, '没有该员工');
//        }
//        $depart_id = $staffOne['depart_id'];
//        $departOne = (array) self::dbConn(self::$dbStDepart)
//            ->where('id', $depart_id)
//            ->first();
//        $depart_name = $departOne['title'];
//        $configWhOnes = self::dbConn(self::$dbConfigWorkHour)
//            ->where('del', 0)
//            ->where('depart_id', $depart_id)
//            ->get();
//        if (!count($configWhOnes)) {
//            return self::toback(-2, '没有工时配置');
//        }
//        //标题数据
//        $title[] = '员工';
//        $title[] = '部门';
//        $title[] = '工时分配';
//        $month = strlen($month)==1 ? '0'.$month : $month;
//        $days = [];
//        $heji = [];
//        $day_num = date('t', strtotime($year.'-'.$month.'-01'));
//        for ($i = 1; $i <= $day_num; $i ++) {
//            $title[] = $i;
//            $days[] = $i;
//            $heji[$i] = 0;
//        }
//        $title[] = '合计';
//        $arys['data'][] = $title;
//        $heji_total = 0;
//        foreach ($configWhOnes as $v) {
//            $tlist = [$staffOne['title'], $depart_name, $v->title];
//            $month_total = 0;
//            foreach ($days as $day) {
//                $day_str = strlen($day)==1 ? '0'.$day : $day;
//                $daystr = $year.'-'.$month.'-'.$day_str;
//                $daystart = $daystr.' 00:00:01';
//                $dayend = $daystr.' 23:59:59';
//                $delay = self::dbConn(self::$dbYwWhItem)
//                    ->where('del', 0)
//                    ->where('uid', $staff_id)
//                    ->where('config_whid', $v->id)
//                    ->where('uptime', '>=', $daystart)
//                    ->where('uptime', '<=', $dayend)
//                    ->sum('delay');
//                $total = number_format($delay / 60, 1);
//                $heji[$day] += $delay;
//                $heji_total += $delay;
//                $tlist[] = $total;
//                $month_total += $total;
//            }
//            $tlist[] = $month_total;
//            $arys['data'][] = $tlist;;
//        }
//        $uid_heji = [
//            $staffOne['title'], $depart_name, '合计',
//        ];
//        foreach ($heji as $v) {
//            $uid_heji[] = number_format($v / 60, 1);
//        }
//        $uid_heji[] = number_format($heji_total / 60, 1);
//        $arys['data'][] = $uid_heji;
//        $arys['staff_name'] = $staffOne['title'];
//        return self::toback(0, '', $arys);
//    }

//    //获取员工月底数据
//    public static function staffMonthList($staff_id,  $year, $month)
//    {
//        $staffOne = (array) self::dbConn(self::$dbStaff)
//            ->where('is_able', 0)
//            ->where('id', $staff_id)
//            ->first();
//        if (empty($staffOne)) {
//            return self::toback(-2, '没有该员工');
//        }
//        $depart_id = $staffOne['depart_id'];
//        $itemOnes = self::dbConn(self::$dbYwWhItem)
//            ->where('del', 0)
//            ->where('depart_id', $depart_id)
//            ->groupBy('config_whid')
//            ->get();
//        if (!count($itemOnes)) {
//            return self::toback(-2, '没有数据');
//        }
//        //标题数据
//        $title[] = '员工';
//        $title[] = '项目名称';
//        $title[] = '工时分配';
//        $month = strlen($month)==1 ? '0'.$month : $month;
//        $days = [];
//        $heji = [];
//        $day_num = date('t', strtotime($year.'-'.$month.'-01'));
//        for ($i = 1; $i <= $day_num; $i ++) {
//            $title[] = $i;
//            $days[] = $i;
//            $heji[$i] = 0;
//        }
//        $title[] = '合计';
//        $heji_total = 0;
//        $item_name = '';
//        $arys['data'][] = $title;
//        foreach ($itemOnes as $v) {
//            $whOne = (array) self::dbConn(self::$dbYwWh)
//                ->where('id', $v->wh_id)
//                ->first();
//            $item_id = empty($whOne) ? '-' : $whOne['item_id'];
//            $itemOne = (array) self::dbConn(self::$dbConfigItem)
//                ->where('id', $item_id)
//                ->first();
//            $item_name = empty($itemOne) ? '-' : $itemOne['title'];
//            $configOne = (array) self::dbConn(self::$dbConfigWorkHour)
//                ->where('id', $v->config_whid)
//                ->first();
//            $config_name = empty($configOne) ? '-' : $configOne['title'];
//            $tlist = [$staffOne['title'], $item_name, $config_name];
//            $month_total = 0;
//            foreach ($days as $day) {
//                $day_str = strlen($day)==1 ? '0'.$day : $day;
//                $daystr = $year.'-'.$month.'-'.$day_str;
//                $daystart = $daystr.' 00:00:01';
//                $dayend = $daystr.' 23:59:59';
//                $delay = self::dbConn(self::$dbYwWhItem)
//                    ->where('del', 0)
//                    ->where('uid', $staff_id)
//                    ->where('config_whid', $v->config_whid)
//                    ->where('uptime', '>=', $daystart)
//                    ->where('uptime', '<=', $dayend)
//                    ->sum('delay');
//                $total = number_format($delay / 60, 1);
//                $heji[$day] += $delay;
//                $heji_total += $delay;
//                $tlist[] = $total;
//                $month_total += $total;
//            }
//            $tlist[] = $month_total;
//            $arys['data'][] = $tlist;;
//        }
//        $uid_heji = [
//            $staffOne['title'], $item_name, '合计',
//        ];
//        foreach ($heji as $v) {
//            $uid_heji[] = number_format($v / 60, 1);
//        }
//        $uid_heji[] = number_format($heji_total / 60, 1);
//        $arys['data'][] = $uid_heji;
//        $arys['staff_name'] = $staffOne['title'];
//        return self::toback(0, '', $arys);
//    }

    //获取员工月底数据
    public static function staffMonthList($depart_id, $staff_id,  $year, $month)
    {
        $departOne = (array) self::dbConn(self::$dbStDepart)
            ->where('del', 0)
            ->where('id', $depart_id)
            ->first();
        if (empty($departOne)) {
            return self::toback(-2, '部门必须的');
        }
        //标题数据
        $arys = [];
        $header = ['部门名称：'.$departOne['title'], '', '', '月份：'.$year.'年'.$month.'月'];
        for ($i = 1; $i < 30; $i ++) {
            $header[] = '';
        }
        $arys['data'][] = $header;
        $titles[] = '员工姓名';
        $titles[] = '项目名称';
        $titles[] = '工时分配';
        $month = strlen($month)==1 ? '0'.$month : $month;
        $days = [];
        $day_num = date('t', strtotime($year.'-'.$month.'-01'));
        for ($i = 1; $i <= $day_num; $i ++) {
            $titles[] = $i;
            $days[] = $i;
            $heji[$i] = 0;
        }
        $titles[] = '合计';
        $arys['data'][] = $titles;
        $staffOnes = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_id', $depart_id)
            ->get();
        if (count($staffOnes)) {
            foreach ($staffOnes as $staffOne) {
                $uid = $staffOne->id;
                $uname = $staffOne->title;
                $heji = [];
                $whOnes = self::dbConn(self::$dbYwWh)
                    ->where('del', 0)
                    ->where('staff_id', $uid)
                    ->groupBy('item_id')
                    ->get();
                if (count($whOnes)) {
                    $is_empty = 0;
                    foreach ($whOnes as $whOne) {
                        $cfgItemOne = (array) self::dbConn(self::$dbConfigItem)
                            ->where('id', $whOne->item_id)
                            ->first();
                        $item_name = empty($cfgItemOne) ? '' : $cfgItemOne['title'];
                        $heji_total = 0;
                        $cfgOnes = self::dbConn(self::$dbYwWhItem)
                            ->where('del', 0)
                            ->where('wh_id', $whOne->id)
                            ->groupBy('config_whid')
                            ->get();
                        if (count($cfgOnes)) {
                            foreach ($cfgOnes as $cfgOne) {
                                $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                                    ->where('id', $cfgOne->config_whid)
                                    ->first();
                                $config_name = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                                $tlist = [$uname, $item_name, $config_name];
                                $month_total = 0;
                                foreach ($days as $day) {
                                    $heji[$day] = isset($heji[$day]) ? $heji[$day] : 0;
                                    $day_str = strlen($day)==1 ? '0'.$day : $day;
                                    $daystr = $year.'-'.$month.'-'.$day_str;
                                    $daystart = $daystr.' 00:00:01';
                                    $dayend = $daystr.' 23:59:59';
                                    $delay = self::dbConn(self::$dbYwWhItem)
                                        ->where('del', 0)
                                        ->where('uid', $uid)
                                        ->where('config_whid', $cfgOne->config_whid)
                                        ->where('uptime', '>=', $daystart)
                                        ->where('uptime', '<=', $dayend)
                                        ->sum('delay');
                                    $total = number_format($delay / 60, 1);
                                    $heji[$day] += $delay;
                                    $heji_total += $delay;
                                    $tlist[] = $total;
                                    $month_total += $total;
                                    $is_empty = 1;
                                }
                                $tlist[] = $month_total;
                                $arys['data'][] = $tlist;
                            }
                        }
                    }
                    if ($is_empty) {
                        $u_heji = [$uname, $item_name, '合计'];
                        $u_delay = 0;
                        foreach ($days as $day) {
                            $day_heji = $heji[$day];
                            $day_heji_delay = number_format($day_heji / 60, 1);
                            $u_heji[] = $day_heji_delay;
                            $u_delay += $day_heji;
                        }
                        $u_total = number_format($u_delay / 60, 1);
                        $u_heji[] = $u_total;
                        //ddd($days, $heji, $u_heji);
                        $arys['data'][] = $u_heji;
                     }
                }
            }
        }
        $arys['depart_name'] = $departOne['title'];
        return self::toback(0, '', $arys);
    }
}